﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visualizator;
using Descriptions_DomainModel.Model;
using CommonClasses;
using Eco.Services;
using CommonClasses.Services;
using Eco.Handles;
using VMLua;
using Eco.ObjectRepresentation;
using VisualDM.Model;
using CommonClasses.Logger;
using Axiom;
using Axiom.Core;
using Axiom.Math;

namespace ModellingModule
{
    public class ModellingModule : IConnection
    {
        /// <summary>
        /// Доступ к сервисам ECO
        /// </summary>
        private IEcoServiceProvider ecoServiceProvider = null;

        /// <summary>
        /// Виртуальная машина Lua
        /// </summary>
        private IVirtualMachineService luaVM;


        #region Modelling Speeds' fields, methods
        private float SpeedEnviroment = 2.0f;
        private float SpeedRotation = 1.0f;
        private float SpeedTranslation = 0.03f;

        public void SetSpeedEnviroment(float speed)
        {
            SpeedEnviroment = speed;
        }

        public void SetSpeedRotation(float speed)
        {
            SpeedRotation = speed;
        }

        public void SetSpeedTranslation(float speed)
        {
            SpeedTranslation = speed;
        }

        public float GetSpeedEnviroment()
        {
            return SpeedEnviroment;
        }

        public float GetSpeedRotation()
        {
            return SpeedRotation * SpeedEnviroment;
        }

        public float GetSpeedTranslation()
        {
            return SpeedTranslation * SpeedEnviroment;
        }
        #endregion


        #region IConnection Members

        public event PinStateChangedHandler OnPinStateChanged;

        public void OnPin(int virtualPinOut, int signal)
        {
            return;
        }

        public void FlushPins()
        {
            return;
        }

        public void Dispose()
        {
            return;
        }

        #endregion


        /// <summary>
        /// Конструктор модуля моделирования
        /// </summary>
        /// <remarks>
        /// Служит только для того, чтобы из анимаций вызывался метод PinStateChanged и передавались наверх супервизорам
        /// </remarks>
        /// <param name="ecoServiceProvider">Доступ к сервисам ECO</param>
        public ModellingModule(IEcoServiceProvider ecoServiceProvider)
        {
            this.ecoServiceProvider = ecoServiceProvider;
            luaVM = ecoServiceProvider.GetEcoService<IVirtualMachineService>();
            luaVM.RegisterObject("ModellingModule", this);
        }

        public void PinStateChanged(int virtualPin, int newState)
        {
            //OnPin(virtualPin, newState);
            Console.WriteLine("<ModellingModule.PinStateChanged>: датчик {0}:{1}", virtualPin, newState);
            if (OnPinStateChanged == null)
                Console.WriteLine("Error: Modelling.PinStateChanged == null");
            else OnPinStateChanged(virtualPin, newState);
        }
    }
}
